package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.AssignMapper;
import com.jd.finance.core.mapper.NodeMapper;
import com.jd.finance.core.mapper.RoleMapper;
import com.jd.finance.core.mapper.UserMapper;
import com.jd.finance.core.pojo.entity.Assign;
import com.jd.finance.core.pojo.entity.Role;
import com.jd.finance.core.pojo.entity.User;
import com.jd.finance.core.service.AssignService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户角色中间表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-25
 */
@Service
public class AssignServiceImpl extends ServiceImpl<AssignMapper, Assign> implements AssignService {

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;

    @Resource
    private AssignMapper assignMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private OpreateLogService opreateLogService;

    @Resource
    private NodeMapper nodeMapper;

    /**
     * 添加rbac中间表 用户-角色
     * @param uid 管理员id
     * @param uid1 用户id
     * @param roleId
     * @param request
     * @return
     */
    @Override
    public BaseResult addUserRole(Integer uid, Integer uid1, Integer roleId, HttpServletRequest request) {
        if(uid == null){
            log.error("管理员id为空");
            return BaseResult.error().message("管理员id为空");
        }
        if(uid1 == null){
            log.error("用户id为空");
            return BaseResult.error().message("用户id为空");
        }
        if(roleId == null){
            log.error("角色id为空");
            return BaseResult.error().message("角色id为空");
        }
        //判断传过来的id数据库里是否一样
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("uid",uid1);
        User user = userMapper.selectOne(qw);
        if (user == null){
            log.error("用户id不存在");
            return BaseResult.error().message("用户id不存在");
        }

        QueryWrapper<Role> qw1 = new QueryWrapper<>();
        qw1.eq("role_id",roleId);
        Role role = roleMapper.selectOne(qw1);
        if (role == null){
            log.error("角色id不存在");
            return BaseResult.error().message("角色id不存在");
        }

        Assign assign = new Assign();
        assign.setUid(uid1);
        assign.setRoleId(roleId);

        int i = assignMapper.insert(assign);
        if (i == 0){
            log.error("添加失败");
            return BaseResult.error().message("添加失败");
        }

        //添加操作日志
        boolean add = opreateLogService.add(uid, "添加", "添加用户-角色中间表", IpUtil.getIpAddr(request));
        if (!add){
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }
        return BaseResult.ok().data("tianjiajieguo:",i);
    }

    /**
     * 根据uid删除RBAC中间表
     * @param uid
     * @param leaveId
     * @param request
     * @return
     */
    @Override
    public BaseResult deleteUserRole(Integer uid, Integer leaveId, HttpServletRequest request) {
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if(leaveId == null){
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }

        QueryWrapper<Assign> qw = new QueryWrapper<>();
        qw.eq("uid",leaveId)
                .select("assi_id");

        Assign assign = new Assign();
        assign.setUid(leaveId);
        int i = assignMapper.delete(qw);
        if (i == 0){
            log.error("删除失败");
            return BaseResult.error().message("删除失败");
        }

        //添加操作日志
        boolean add = opreateLogService.add(uid, "删除", "删除用户-角色中间表", IpUtil.getIpAddr(request));
        if (!add){
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }
        return BaseResult.ok().data("shanchujieguo",i);
    }


}
